package com.woniuxy.education.controller;


import com.woniuxy.education.entity.HomeworkAssignments;
import com.woniuxy.education.entity.HomeworkTypeNumber;
import com.woniuxy.education.entity.TestMessage;
import com.woniuxy.education.service.HomeworkAssignmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@CrossOrigin
@RestController
public class HomeworkAssignmentsController {
    @Autowired
    HomeworkAssignmentsService homeworkAssignmentsService;
    @RequestMapping("/teacher/assign_homework")
    //布置作业
    public String insertHomework(HomeworkAssignments homeworkAssignments,
                                 HomeworkTypeNumber homeworkTypeNumber){

        //设置作业状态
       homeworkAssignments.setStatus("未完成");
        Calendar cd = Calendar.getInstance();
        //根据作业类型生成相应的完成时间
        String type=homeworkAssignments.getJob_type();
        if("课后作业".equals(type)){
            //当天完成,发布作业加12小时
            // cd.add(Calendar.DAY_OF_WEEK,1);
            cd.add(Calendar.HOUR,12);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //日加7天
            String format = formatter.format(cd.getTime());
            homeworkAssignments.setExpiration_time(format);
        }
        if("周末作业".equals(type)){
            //周内完成,加3天
            cd.add(Calendar.DAY_OF_WEEK,3);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //日加7天
            String format = formatter.format(cd.getTime());
            homeworkAssignments.setExpiration_time(format);
        }
        if("寒暑假作业".equals(type)){
            //假期内完成，加60天
             cd.add(Calendar.DAY_OF_WEEK,60);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //日加7天
            String format = formatter.format(cd.getTime());
            homeworkAssignments.setExpiration_time(format);
        }
        //生成作业id
       // homeworkAssignments.setId(UUID.randomUUID().toString().replace("-", ""));
      try {

          //添加作业
          homeworkAssignmentsService.insertHomework(homeworkAssignments, homeworkTypeNumber);
      }catch (Exception e){
          System.out.println(e);
          return "201";
      }
        return "200";
    }


    @RequestMapping("/teacher/paper_upload")
    //试卷上传
    public String insertTest(String term,String type,String userid,String endtime){

        type=term+type;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String format = formatter.format(new Date());
        endtime=format+" "+endtime;
        TestMessage testMessage=new TestMessage();
        testMessage.setType(type);
        testMessage.setEnd_time(endtime);
        //System.out.println(type);
       // System.out.println(endtime);

        try {
            //添加试卷
            homeworkAssignmentsService.insertTest(testMessage, userid);
        }catch (Exception e){
            System.out.println(e);
            System.out.println("201");
            return "201";
        }
        return "200";
    }
}
